<?php
require ".".DIRECTORY_SEPARATOR."vendor".DIRECTORY_SEPARATOR."autoload.php";

use PhpAmqpLib\Connection\AMQPStreamConnection;

use PhpAmqpLib\Message\AMQPMessage;

//声明连接参数
$conf = [
    'host' => PHP_OS == "Linux" ? '127.0.0.1' : '10.0.0.200',
    'vhost' => '/', //类比数据库名称
    'port' => 5672,
    'user' => 'wyqgg',
    'pwd' => '123456',
];
//连接broker

$exchangeName = 'kd_sms_send_ex'; //交换机名

$queueName = 'kd_sms_send_q'; //队列名称

$routingKey = 'sms_send'; //路由关键字(也可以省略)

$conn = new AMQPStreamConnection( //建立生产者与mq之间的连接

    $conf['host'], $conf['port'], $conf['user'], $conf['pwd'], $conf['vhost']

);


if (!$conn) {
    echo "连接失败";
    exit();
}

$channel = $conn->channel(); //在已连接基础上建立生产者与mq之间的通道


$channel->exchange_declare($exchangeName, 'direct', false, true, false); //声明初始化交换机

$channel->queue_declare($queueName, false, true, false, false); //声明初始化一条队列

$channel->queue_bind($queueName, $exchangeName, $routingKey); //将队列与某个交换机进行绑定，并使用路由关键字


for ($i = 0; $i < 10; ++$i) {
    //消息内容
    $messageBody = "Hello,wyqGG Now Time:" . date("h:i:s");
    //将我们需要的消息标记为持久化 - 通过设置AMQPMessage的参数delivery_mode = AMQPMessage::DELIVERY_MODE_PERSISTENT
    $message = new AMQPMessage($messageBody, array('content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT));
    //发送消息
    $channel->basic_publish($message, $exchangeName, $routingKey);
    echo "发送消息:" . $i . "\n";
}

$channel->close();

$conn->close();


